<template>
  <div style="margin-top:20px">
    <el-form ref="form" label-width="120px" :model="form">
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="名称" style="margin-right:15px">
              <el-input v-model="form.name" :disabled="true" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="助记符" style="margin-right:15px">
              <el-input v-model="form.mnemonic" :disabled="true" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="商品类型" style="margin-right:15px">
              <el-cascader v-model="type" :options="data" :show-all-levels="false" style="width: 100%" :disabled="true" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="规格" style="margin-right:15px">
              <el-input v-model="form.spec" :disabled="true" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="批号" style="margin-right:15px">
              <el-input v-model="form.lotNumber" :disabled="true" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="条码" style="margin-right:15px">
              <el-input v-model="form.barCode" :disabled="true" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="单位" style="margin-right:15px">
              <unitElSelect ref="unitName" :options="data" :show-all-levels="false" style="width: 100%" :isDisabled="true" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="辅助单位" style="margin-right:15px">
              <auxiliaryUnitElSelect ref="auxiliaryUnitName" style="width: 100%;"  @queryUnitCode="selectAuxiliaryUnitName" :isDisabled="true" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="最低库存量预警" style="margin-right:15px">
              <el-input v-model="form.miniStock" :disabled="true" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="最高库存量预警" style="margin-right:15px">
              <el-input v-model="form.maxStock" :disabled="true" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import request from '@/utils/request'
import unitElSelect from './unitElSelect.vue'
import auxiliaryUnitElSelect from './auxiliaryUnitElSelect.vue'
Vue.use(VXETable)
export default {
  name: 'GoodsDetail',
  components: {
    unitElSelect,
    auxiliaryUnitElSelect
  },
  data() {
    return {
      emptyForm: {},
      data: [],
      form: {
        code: '',
        name: '',
        mnemonic: '',
        typeCode: '',
        spec: '',
        lotNumber: '',
        barCode: '',
        unitCode: '',
        auxiliaryUnitCode: '',
        miniStock: '',
        maxStock: '',
        type: []
      },
      type: [],
      unitCodeList: [],
      auxiliaryUnitCodeList: []
    }
  },
  created() {
    // 保存空表单对象
    this.emptyForm = JSON.parse(JSON.stringify(this.form))
  },
  methods: {
    selectUnitCode(value) {
      this.form.unitCode = value
    },
    selectAuxiliaryUnitName(value) {
      this.form.auxiliaryUnitCode = value
    },
    findGoodsTypeList() {
      this.loading = true
      request({ url: '/arpa-basic-api/goodsType/queryTreeData', method: 'get', params: { level: 1 }}).then(response => {
        this.data = response.data
      })
    },
    // 获取页面信息
    getGoodsTypeInfo(code) {
      request({ url: `/arpa-basic-api/goodsType/${code}`, method: 'get' }).then(response => {
        if (response.data.level >= 1) {
          this.type.unshift(response.data.code)
          this.getGoodsTypeInfo(response.data.parentCode)
        }
      })
    },
    // 获取页面信息
    getInfo(code, data, unitCodeList, auxiliaryUnitCodeList) {
      this.data = data
      this.unitCodeList = unitCodeList
      this.auxiliaryUnitCodeList = auxiliaryUnitCodeList
      this.data = data
      // 查询到数据之前先展示空表单
      this.form = JSON.parse(JSON.stringify(this.emptyForm))
      request({ url: `/arpa-basic-api/goods/${code}`, method: 'get' }).then(response => {
        this.form = response.data
        this.$refs.unitName.setSelectValue(this.form.unitCode)
        this.$refs.auxiliaryUnitName.setSelectValue(this.form.auxiliaryUnitCode)
        this.getGoodsTypeInfo(this.form.typeCode)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
    .dialog-footer {
        margin-top: 50px;
    }
</style>
